package hackathon.techbooster.kuraudokakeibo;

import java.util.Calendar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {

	private Calendar calendar;
	private ListView listView;
	private ArrayAdapter<String> adapter;
	private Handler handler;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        calendar = Calendar.getInstance();
        handler = new Handler();
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
        listView = (ListView)findViewById(R.id.listview);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            	Intent intent = new Intent(MainActivity.this, DetailActivity.class);
            	intent.setAction(Intent.ACTION_VIEW);
            	startActivity(intent);
            }
        });
        updateListView();

        // 前
        Button prevButton  = (Button)findViewById(R.id.prevButton);
        prevButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				changePrevMonth();
			}
		});

        // 次
        Button nextButton  = (Button)findViewById(R.id.nextButton);
        nextButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				changeNextMonth();
			}
		});
    }

    /**
     * onCreateOptionsMenu
     */
    @Override
    public boolean onCreateOptionsMenu(android.view.Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    /**
     * onOptionsItemSelected
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_regist:
        {
        	Intent intent = new Intent(MainActivity.this, TourokuActivity.class);
        	intent.setAction(Intent.ACTION_VIEW);
        	startActivity(intent);
            break;
        }
        default:
        {
            break;
        }
        }

        return true;
    }

    private void changePrevMonth() {
    	calendar.add(Calendar.MONDAY, -1);
    	updateListView();
    }

    private void changeNextMonth() {
    	calendar.add(Calendar.MONDAY, +1);
    	updateListView();
    }

    private void updateListView() {
    	int month = calendar.get(Calendar.MONTH) + 1; // 月は0からなので+1
    	int monthDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

    	// 日付分登録
    	adapter.clear();
    	for(int i = 0; i < monthDay ; i++) {
    		adapter.add(month + "月" + (i + 1) + "日");
    	}

        handler.post(new Runnable() {
			@Override
			public void run() {
				adapter.notifyDataSetChanged();
				listView.setSelection(0);
			}
        });
    }
}